﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class UsersService
    {
        //注册及体验金新增
        public static bool AddTYUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Name", us.Us_Name), new SqlParameter("@Us_Sex", us.Us_Sex),
            new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),new SqlParameter("@Us_phone",us.Us_Phone),
            new SqlParameter("@Us_Balance",us.Us_Balance),new SqlParameter("@Us_Goid",us.Us_Goid)};
            return DBHelper.Instance().ExcuteProcedure("up_AddTYUsers", para);
        }
        //新增
        public static bool AddUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Name", us.Us_Name), new SqlParameter("@Us_Sex", us.Us_Sex),
            new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),new SqlParameter("@Us_phone",us.Us_Phone)};
            return DBHelper.Instance().ExcuteProcedure("up_AddUsers", para);
        }
        //注册新增
        public static bool AddZCUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { 
            new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),new SqlParameter("@Us_phone",us.Us_Phone)};
            return DBHelper.Instance().ExcuteProcedure("up_AddZCUsers", para);
        }
        
        //删除
        public static bool DeleUsers(int id)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Id",id) };
            return DBHelper.Instance().ExcuteProcedure("up_DeleUsers", para);
        }

        //修改全部内容
        public static bool UpdaUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Name", us.Us_Name), new SqlParameter("@Us_Sex", us.Us_Sex),
            new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),new SqlParameter("@Us_phone",us.Us_Phone),
            new SqlParameter("@Us_Balance",us.Us_Balance),new SqlParameter("@Us_Goid",us.Us_Goid),new SqlParameter("@Us_Image",us.Us_Image),
            new SqlParameter("@Us_qhdo",us.Us_Qhdo),new SqlParameter("@Us_Ident",us.Us_Ident),new SqlParameter("@Us_Id",us.Us_Id)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaUsers", para);
        }
        
        //修改部分内容
        public static bool UpdaPartUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Name", us.Us_Name), new SqlParameter("@Us_Sex", us.Us_Sex),
            new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),new SqlParameter("@Us_phone",us.Us_Phone),
            new SqlParameter("@Us_Balance",us.Us_Balance),new SqlParameter("@Us_Goid",us.Us_Goid),new SqlParameter("@Us_Id",us.Us_Id)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaPartUsers", para);
        }

        //修改余额和金币
        public static bool UpdaPriceUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Balance",us.Us_Balance),new SqlParameter("@Us_Goid",us.Us_Goid),
                new SqlParameter("@Us_Id",us.Us_Id)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaPriceUsers", para);
        }
        //找回密码
        public static bool UpdaZHMMUsers(Users us)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Us_Account",us.Us_Account),new SqlParameter("@Us_Password",us.Us_Password),
                new SqlParameter("@Us_phone",us.Us_Phone),new SqlParameter("@Us_Id",us.Us_Id)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaZHMMUsers", para);
        }

        //查询所有用户信息
        public static List<Users> SeleAllUsers()
        {
            List<Users> item = new List<Users>();
            string sql = string.Format("select * from Users ");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Users us = new Users();
                    us.Us_Id = Convert.ToInt32(dt.Rows[i]["Us_Id"].ToString());
                    us.Us_Name = dt.Rows[i]["Us_Name"].ToString();
                    us.Us_Sex = Convert.ToChar(dt.Rows[i]["Us_Sex"].ToString());
                    us.Us_Account = dt.Rows[i]["Us_Account"].ToString();
                    us.Us_Password = dt.Rows[i]["Us_Password"].ToString();
                    us.Us_Phone = dt.Rows[i]["Us_Phone"].ToString();
                    us.Us_Balance = Convert.ToDouble(dt.Rows[i]["Us_Balance"].ToString());
                    us.Us_Goid = Convert.ToInt32(dt.Rows[i]["Us_Goid"].ToString());
                    us.Us_Image = dt.Rows[i]["Us_Image"].ToString();
                    us.Us_Qhdo = Convert.ToInt32(dt.Rows[i]["Us_Qhdo"].ToString());
                    us.Us_Ident = Convert.ToInt32(dt.Rows[i]["Us_Ident"].ToString());
                    item.Add(us);
                }
            }
            return item;
        }
        //判断用户是否存在
        public static List<Users> SelePDCZUsers(string name,string pwd)
        {
            List<Users> item = new List<Users>();
            string sql = string.Format("select * from Users where Us_Account='{0}' and Us_Password='{1}'", name,pwd);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Users us = new Users();
                    us.Us_Id = Convert.ToInt32(dt.Rows[i]["Us_Id"].ToString());
                    us.Us_Name = dt.Rows[i]["Us_Name"].ToString();
                    us.Us_Sex = Convert.ToChar(dt.Rows[i]["Us_Sex"].ToString());
                    us.Us_Account = dt.Rows[i]["Us_Account"].ToString();
                    us.Us_Password = dt.Rows[i]["Us_Password"].ToString();
                    us.Us_Phone = dt.Rows[i]["Us_Phone"].ToString();
                    us.Us_Balance = Convert.ToDouble(dt.Rows[i]["Us_Balance"].ToString());
                    us.Us_Goid = Convert.ToInt32(dt.Rows[i]["Us_Goid"].ToString());
                    us.Us_Image = dt.Rows[i]["Us_Image"].ToString();
                    us.Us_Qhdo = Convert.ToInt32(dt.Rows[i]["Us_Qhdo"].ToString());
                    us.Us_Ident = Convert.ToInt32(dt.Rows[i]["Us_Ident"].ToString());
                    item.Add(us);
                }
            }
            return item;
        }
        //找回密码判断用户是否存在
        public static List<Users> SelePwdZhaoHuiUsers(string name, string phone)
        {
            List<Users> item = new List<Users>();
            string sql = string.Format("select * from Users where Us_Account='{0}' and Us_Phone='{1}'", name, phone);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Users us = new Users();
                    us.Us_Id = Convert.ToInt32(dt.Rows[i]["Us_Id"].ToString());
                    us.Us_Name = dt.Rows[i]["Us_Name"].ToString();
                    us.Us_Sex = Convert.ToChar(dt.Rows[i]["Us_Sex"].ToString());
                    us.Us_Account = dt.Rows[i]["Us_Account"].ToString();
                    us.Us_Password = dt.Rows[i]["Us_Password"].ToString();
                    us.Us_Phone = dt.Rows[i]["Us_Phone"].ToString();
                    us.Us_Balance = Convert.ToDouble(dt.Rows[i]["Us_Balance"].ToString());
                    us.Us_Goid = Convert.ToInt32(dt.Rows[i]["Us_Goid"].ToString());
                    us.Us_Image = dt.Rows[i]["Us_Image"].ToString();
                    us.Us_Qhdo = Convert.ToInt32(dt.Rows[i]["Us_Qhdo"].ToString());
                    us.Us_Ident = Convert.ToInt32(dt.Rows[i]["Us_Ident"].ToString());
                    item.Add(us);
                }
            }
            return item;
        }
        //查指定定id用户信息
        public static List<Users> SeleIdUsers(int id)
        {
            List<Users> item = new List<Users>();
            string sql = string.Format("select * from Users where Us_Id='{0}'",id);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if(dt!=null && dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Users us = new Users();
                    us.Us_Id = Convert.ToInt32(dt.Rows[i]["Us_Id"].ToString());
                    us.Us_Name = dt.Rows[i]["Us_Name"].ToString();
                    us.Us_Sex = Convert.ToChar(dt.Rows[i]["Us_Sex"].ToString());
                    us.Us_Account = dt.Rows[i]["Us_Account"].ToString();
                    us.Us_Password = dt.Rows[i]["Us_Password"].ToString();
                    us.Us_Phone = dt.Rows[i]["Us_Phone"].ToString();
                    us.Us_Balance = Convert.ToDouble(dt.Rows[i]["Us_Balance"].ToString());
                    us.Us_Goid = Convert.ToInt32(dt.Rows[i]["Us_Goid"].ToString());
                    us.Us_Image = dt.Rows[i]["Us_Image"].ToString();
                    us.Us_Qhdo = Convert.ToInt32(dt.Rows[i]["Us_Qhdo"].ToString());
                    us.Us_Ident = Convert.ToInt32(dt.Rows[i]["Us_Ident"].ToString()); 
                    item.Add(us);
                }
            }
            return item;
        }
        
        //查指定姓名用户信息
        public static List<Users> SeleNameUsers(string Name)
        {
            List<Users> item = new List<Users>();
            string sql = string.Format("select * from Users where Us_Name like '%{0}%'", Name);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Users us = new Users();
                    us.Us_Id = Convert.ToInt32(dt.Rows[i]["Us_Id"].ToString());
                    us.Us_Name = dt.Rows[i]["Us_Name"].ToString();
                    us.Us_Sex = Convert.ToChar(dt.Rows[i]["Us_Sex"].ToString());
                    us.Us_Account = dt.Rows[i]["Us_Account"].ToString();
                    us.Us_Password = dt.Rows[i]["Us_Password"].ToString();
                    us.Us_Phone = dt.Rows[i]["Us_Phone"].ToString();
                    us.Us_Balance = Convert.ToDouble(dt.Rows[i]["Us_Balance"].ToString());
                    us.Us_Goid = Convert.ToInt32(dt.Rows[i]["Us_Goid"].ToString());
                    us.Us_Image = dt.Rows[i]["Us_Image"].ToString();
                    us.Us_Qhdo = Convert.ToInt32(dt.Rows[i]["Us_Qhdo"].ToString());
                    us.Us_Ident = Convert.ToInt32(dt.Rows[i]["Us_Ident"].ToString());
                    item.Add(us);
                }
            }
            return item;
        }

    }
}
